package demo8;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class RobotTrace {
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Point {
        private int x;
        private int y;
    }

    private int rows;
    private int columns;

    /**
     * 向下和向右
     */
    public void getTrace(int x, int y) {
        // x+1 or y+1; => to the 2,2
        if (x > rows) {
            return;
        }
        if (y > columns) {
            return;
        }
        System.out.println("x: " + x + " , y: " + y);
        if (x == rows && y == columns) {
            System.out.println("-------------------split line-------------------");
            return;
        }
        getTrace(x, y + 1);
        getTrace(x + 1, y);
    }

    public GetTraceWithMemo createTraceWithMemo() {
        return new GetTraceWithMemo();
    }

    public class GetTraceWithMemo {

        public void getTaceInitAndStart() {
            ArrayList<Point> points = new ArrayList<>();
            getTrace(points, 0, 0);
            System.out.println(points);
        }
        // arr[2][3]

        /**
         * [0,0] [0,1] [0,2]
         * [1,0] [1,1] [1,2]
         */
        public boolean getTrace(ArrayList<Point> points, int x, int y) {
            // x+1 or y+1; => to the 2,2
            if (x > rows) {
                return false;
            }
            if (y > columns) {
                return false;
            }
            points.add(new Point(x, y));
            if (x == rows && y == columns) {
                System.out.println("-------------------split line-------------------");
                return true;
            }
            if (getTrace(points, x, y + 1) || getTrace(points, x + 1, y)) {
                return true;
            }
            ;
            return false;
        }
    }
    public GetTraceWithMemoAndObstacle createGetTraceWithMemoAndObstacle(){
        return new GetTraceWithMemoAndObstacle();
    }
    public class GetTraceWithMemoAndObstacle {
        public  Random random = new Random();
        // 5 , 3
        boolean[][] booleans = new boolean[rows+1][columns+1];
        private boolean randomRange(int percent){
            // 0,99 => 1,100
            int i = random.nextInt(100) + 1;
            return i<percent;
        }
        private  void randomSetting(){
            int ceil = (int)Math.ceil((double) (rows * columns) / 10);
            while (ceil>0){
                // 4 * 2
                int x = random.nextInt(rows+1);
                int y = random.nextInt(columns+1);
                booleans[x][y]=true;
                ceil--;
            }
        }
        private void randomSetting2(){
            for (int j = 0; j <= rows; j++) {
                for (int k = 0; k <= columns; k++) {
                    booleans[j][k]=!randomRange(70);
                }
                System.out.println();
            }
        }
        public boolean[][] createMatrix(){
            randomSetting2();
            booleans[0][0]=false;
            booleans[rows][columns]=false;
            printMatrix(booleans);
            return booleans;
        }
        private void printMatrix(boolean[][] matrix){
            for (int j = 0; j <= rows; j++) {
                for (int k = 0; k <= columns; k++) {
                    System.out.print(matrix[j][k]);
                }
                System.out.println();
            }
        }
        public void getTaceInitAndStart() {
            ArrayList<Point> points = new ArrayList<>();
            boolean[][] matrix = createMatrix();
            getTrace(points,matrix, 0, 0);
            System.out.println(points);
        }

        /** arr[][] get the arr[x][y]
         * [0,0] [0,1] [0,2]
         * [1,0] [1,1] [1,2]
         */
        public boolean getTrace(ArrayList<Point> points, boolean[][] matrix,int x, int y) {
            // x+1 or y+1; => to the 2,2
            if (x > rows) {
                return false;
            }
            if (y > columns) {
                return false;
            }
            if (matrix[x][y])return false;
            System.out.println("x: " + x + " , y: " + y);
            points.add(new Point(x, y));
            if (x == rows && y == columns) {
                System.out.println("-------------------split line-------------------");
                return true;
            }
            if (getTrace(points, matrix,x, y + 1) || getTrace(points, matrix,x + 1, y)) {
                return true;
            }
            return false;
        }
    }

    public int[][] createPath() {
        // for x=2
        // for y =3
        int[][] arr = new int[5][5];
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                arr[i][j] = 1;
            }
        }
        System.out.println(arr.length);
        return arr;
    }
}
